package com.example.socket.server;

import com.example.socket.client.NettyClient;
import com.example.socket.model.DataPackage;
import com.example.socket.model.FinishDataValue;
import com.example.socket.model.ObjectAndByte;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

@Slf4j
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    /**
     * 客户端连接会触发
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("Channel active......");
    }

    /**
     * 客户端发消息会触发
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof  ByteBuf){
            ByteBuf buf = (ByteBuf)msg;
            int i = buf.readableBytes();
            byte[] bytes = new byte[i];
            buf.getBytes(buf.readerIndex(), bytes);
            Socket socket = new Socket("127.0.0.1",9090);
            OutputStream bos = socket.getOutputStream();
            InputStream in = socket.getInputStream();
            bos.write(bytes);
            bos.flush();
            byte[] bytes1 = new byte[4096];
            int read = in.read(bytes1);
            log.info("收到响应从长度:: {}",read);

            buf.readBytes(4);
            int type = buf.readInt();

            if (type == 1) {
                DataPackage dataPackage1 = new DataPackage();
                dataPackage1.setType(1);
                dataPackage1.setLength(5);
                dataPackage1.setValue("hello".getBytes(StandardCharsets.UTF_8));
                ctx.writeAndFlush(dataPackage1);
                bos.close();
                socket.close();
                return;
            }


            if (type == 2){
                DataPackage dataPackage1 = new DataPackage();
                dataPackage1.setType(3);
                dataPackage1.setLength(7);
                dataPackage1.setValue("data ack".getBytes(StandardCharsets.UTF_8));
                ctx.writeAndFlush(dataPackage1);
                bos.close();
                socket.close();
                return;
            }


            if (type == 4){
                log.info("收到 done 包: ");
                DataPackage dataPackage1 = new DataPackage();
                dataPackage1.setType(5);
                dataPackage1.setLength(0);
                dataPackage1.setValue("finish".getBytes(StandardCharsets.UTF_8));
                ctx.writeAndFlush(dataPackage1);
                bos.close();
                socket.close();
            }

        }
    }

    /**
     * 发生异常触发
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
